/**
 * 封装异步方法提示
 * @param this this
 * @param callback 异步的回调函数
 * @param showSuccess 是否显示调用成功提示
 * @param errorHandle 调用失败时执行的方法
 * @param finalHandle 调用结束后执行的方法
 */
// eslint-disable-next-line max-params
export async function requestedMessage(
  this: any,
  callback: () => Promise<string>,
  showSuccess = true,
  errorHandle?: (error: unknown) => void,
  finalHandle?: () => void
) {
  window.$loading.start();
  showSuccess && window.$message.destroyAll();
  try {
    const msg = await callback();
    window.$loading.finish();
    showSuccess && window.$message.success(msg);
  } catch (error) {
    errorHandle && errorHandle.call(this, error);
    if (typeof error === "string") {
      window.$message.error(error);
    } else {
      window.$message.error("请求出错");
    }
    window.$loading.error();
  } finally {
    finalHandle && finalHandle();
  }
}
